package com.jet.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jet.core.utils.IdUtil;
import com.jet.member.domain.dto.MemberAttrDto;
import com.jet.member.domain.entity.MemberAttr;
import com.jet.member.mapper.MemberAttrMapper;
import com.jet.member.service.IMemberAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-02
 */
@Service
public class MemberAttrServiceImpl extends ServiceImpl<MemberAttrMapper, MemberAttr> implements IMemberAttrService {

    public void save(long merchantNo, MemberAttrDto dto) {
        MemberAttr attr = new MemberAttr();
        attr.setId(IdUtil.generateLower());
        attr.setMerchantNo(merchantNo);
        attr.setRewardModel(dto.getRewardModel());
        attr.setRewardNumber(dto.getRewardNumber());
        attr.setConvertAmount(dto.getConvertAmount());
        attr.setConvertIntegral(dto.getConvertIntegral());
        super.save(attr);
    }

    @Override
    public void edit(long merchantNo, MemberAttrDto dto) {
        MemberAttr attr = findByMerchantNo(merchantNo);
        if (attr != null) {
            attr.setRewardModel(dto.getRewardModel());
            attr.setRewardNumber(dto.getRewardNumber());
            attr.setConvertAmount(dto.getConvertAmount());
            attr.setConvertIntegral(dto.getConvertIntegral());
            super.updateById(attr);
        } else {
            save(merchantNo, dto);
        }
    }

    @Override
    public MemberAttr findByMerchantNo(long merchantNo) {
        Wrapper<MemberAttr> wrapper = new LambdaQueryWrapper<MemberAttr>()
                .eq(MemberAttr::getMerchantNo, merchantNo);
        return super.getOne(wrapper);
    }
}
